package com.hdax.dm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.beans.Scheduler.DmSchedulerSeat;
import com.hdax.dm.mappers.getScheduleMapper;
import com.hdax.dm.service.DmSchedulerSeatService;
import com.hdax.dm.utils.response.CommonResponse;
import com.hdax.dm.utils.response.ResponseUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DmSchedulerSeatServiceImpl extends ServiceImpl<getScheduleMapper, DmSchedulerSeat> implements DmSchedulerSeatService {

    @Override
    public List<com.hdax.dm.beans.Scheduler.DmSchedulerSeat> DmSchedulerSeat(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("scheduleId",id);
        queryWrapper.isNull("orderNo");
        queryWrapper.isNull("userId");
        queryWrapper.eq("status",1);
        List<com.hdax.dm.beans.Scheduler.DmSchedulerSeat> lists = baseMapper.selectList(queryWrapper);
        return lists;
    }

    @Override
    public CommonResponse<Map<String, List<String>>> getSeatList(Map<String, Integer> params) {
        Integer scheduleId = params.get("scheduleId");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("scheduleId",scheduleId);

        List<com.hdax.dm.beans.Scheduler.DmSchedulerSeat> lists = baseMapper.selectList(queryWrapper);
        Map<Long,List<com.hdax.dm.beans.Scheduler.DmSchedulerSeat>> map = new HashMap<>();
        map = lists.stream().collect(Collectors.groupingBy(com.hdax.dm.beans.Scheduler.DmSchedulerSeat::getX));
        List<String> seatArray = new ArrayList<>();
        map.values().forEach(seats ->{
            StringBuffer buffer = new StringBuffer();
            List<com.hdax.dm.beans.Scheduler.DmSchedulerSeat> list = seats;
            list.forEach(seat ->{
                if (seat.getAreaLevel().equals("0")){
                    buffer.append("_");
                }else if (seat.getAreaLevel().equals("1")){
                    buffer.append("a");
                }else if (seat.getAreaLevel().equals("2")){
                    buffer.append("b");
                }else if (seat.getAreaLevel().equals("3")){
                    buffer.append("c");
                }
            });
            seatArray.add(buffer.toString());
        });

        Map<String,List<String>> map1 = new HashMap<>();
        map1.put("seatArray",seatArray);

        return ResponseUtil.returnSuccess(map1);
    }

    @Override
    public DmSchedulerSeat selectzuowei(Long x, Long y,Integer schedulerId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("x",x);
        queryWrapper.eq("y",y);
        queryWrapper.eq("scheduleId",schedulerId);
        queryWrapper.isNull("orderNo");
        queryWrapper.isNull("userId");
        queryWrapper.eq("status",1);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer updateschedulerseat(DmSchedulerSeat dmSchedulerSeat) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("x",dmSchedulerSeat.getX());
        queryWrapper.eq("y",dmSchedulerSeat.getY());
        queryWrapper.eq("scheduleId",dmSchedulerSeat.getScheduleId());

        return baseMapper.update(dmSchedulerSeat,queryWrapper);
    }

    @Override
    public Integer updateseat(String orderNo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("orderNo",orderNo);

        DmSchedulerSeat schedulerSeat = new DmSchedulerSeat();
        schedulerSeat.setUserId(null);
        schedulerSeat.setOrderNo(null);
        schedulerSeat.setStatus(1L);
        return baseMapper.update(schedulerSeat,queryWrapper);
    }
}
